import axios from 'axios'
import { ElMessage } from 'element-plus'

axios.defaults.baseURL = 'http://localhost:8081'

axios.interceptors.request.use(function (config) {
  return config
}, function (error) {
  return Promise.reject(error)
}
)

axios.interceptors.response.use(function (response) {
  const res = response.data
  if (res.code === 200) {
    ElMessage({
      message: res.msg,
      type: 'success',
      duration: 2 * 1000
    })
    return response
  } else {
    // 弹窗显示异常信息
    ElMessage({
      message: response.data.msg,
      type: 'error',
      duration: 10 * 1000
    })
    // 直接拒绝往下面返回结果信息
    return Promise.reject(response.data.msg)
  }
}, function (error) {
  console.log('error：' + error)
  if (error.response.data) {
    error.message = error.response.data.msg
  }
  // 根据请求状态觉得是否登录或者提示其他
  ElMessage({
    message: error.message,
    type: 'error',
    duration: 10 * 1000
  })
  return Promise.reject(error)
})
